package com.itany.thread.test03;

public class Consumer extends Thread{
	private ProductPool pool;
	private String name;
	public Consumer(ProductPool pool,String name) {
		this.pool = pool;
		this.name = name;
	}
	@Override
	public void run() {
		while(true){
			synchronized (pool) {
				if(pool.isEmpty()){
					// 当缓冲区商品为空时,停止消费,等待生产
					try {
						pool.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else{
					int num = pool.get();
					System.out.println(this.name+"消费了一个商品,当前商品数量为:"+num);
					// 当消费者进行了消费之后,缓冲区商品不满,此时需要唤醒生产者进行生产
					pool.notifyAll();
				}
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
